package com.jcfk.system.service;

import com.jcfk.system.api.utils.LoginUserInfoUtils;
import com.jcfk.system.domain.dto.OaKeyworkTaskDTO;
import com.jcfk.system.domain.dto.OaKeyworkTaskindexDTO;
import com.jcfk.system.domain.dto.SysUserDTO;
import com.jcfk.system.domain.po.OaKeyworkTaskindex;
import com.jcfk.system.domain.po.SysAnnexesFile;
import com.jcfk.system.domain.po.SysUser;
import com.jcfk.system.domain.query.SysAnnexesFileQuery;
import com.jcfk.system.domain.dto.SysAnnexesFileDTO;
import com.jcfk.system.dao.SysAnnexesFileDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.system.domain.query.SysUserQuery;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import java.util.Date;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 附件信息
 *
 * @author libinghai
 * @email
 * @date 2023-12-13
 */
@Service
public class SysAnnexesFileService extends BaseService<SysAnnexesFileDao, SysAnnexesFile> {

    /**
     * SysAnnexesFile分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<SysAnnexesFileDTO> queryByPage(DataPage<SysAnnexesFileDTO> page, SysAnnexesFileQuery query) {
        // 分页查询
        query.setCreatedUserName(StringUtils.isEmpty(query.getCreatedUserName()) ? query.getCreatedUserName() : query.getCreatedUserName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }

    /**
     * 根据条件获取文件信息列表
     *
     * @param query 查询条件
     * @return List<SysAnnexesFileDTO>
     */
    public List<SysAnnexesFileDTO> getList(SysAnnexesFileQuery query) {
        // 查询条件
        QueryWrapper<SysAnnexesFile> qw = new QueryWrapper<>();
        if (!StrUtil.isEmpty(query.getOwnerType())) {
            qw.lambda().eq(SysAnnexesFile::getOwnerType, query.getOwnerType());
        }
        if (!StrUtil.isEmpty(query.getOwnerId())) {
            qw.lambda().eq(SysAnnexesFile::getOwnerId, query.getOwnerId());
        }

        // 查询数据
        List<SysAnnexesFile> list = super.list(qw);
        List<SysAnnexesFileDTO> dtoList = new ArrayList<>();
        for (SysAnnexesFile po : list) {
            SysAnnexesFileDTO dto = new SysAnnexesFileDTO();
            BeanUtils.copyProperties(po, dto);
            dtoList.add(dto);
        }
        return dtoList;
    }

    /**
     * 判断SysAnnexesFile合法性
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(SysAnnexesFileDTO dto) {
        QueryWrapper<SysAnnexesFile> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 判断文件是否存在
     *
     * @param fileName
     * @return
     */
    public RetResult<Boolean> existsFile(String fileName) {
        QueryWrapper<SysAnnexesFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysAnnexesFile::getFilename, fileName);

        int count = this.baseMapper.selectCount(queryWrapper);

        return RetResult.ok(count > 0 ? true : false);
    }

    /**
     * 插入附件信息
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysAnnexesFileDTO dto) {

        // 保存
        SysAnnexesFile item = new SysAnnexesFile();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新附件信息
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(SysAnnexesFileDTO dto) {

        // 保存
        SysAnnexesFile item = new SysAnnexesFile();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(SysAnnexesFile item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreatedUserName(SecurityUtils.getUserName());
            item.setCreateTime(new Date());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());
        item.setLastUpdateTime(new Date());
    }

    /**
     * 根据主键获取附件信息信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public SysAnnexesFileDTO get(String id) {
        // 查询数据
        SysAnnexesFile item = this.baseMapper.selectById(id);
        SysAnnexesFileDTO dto = new SysAnnexesFileDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * @param dtos
     * @return void
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<SysAnnexesFileDTO> dtos, String ownerId, String ownerType) {
        List<SysAnnexesFile> insertList = new ArrayList<>();

        QueryWrapper<SysAnnexesFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysAnnexesFile::getOwnerId, ownerId);
        queryWrapper.lambda().eq(SysAnnexesFile::getOwnerType, ownerType);

        // 默认值填充
        for (SysAnnexesFileDTO dto : dtos) {
            SysAnnexesFile item = new SysAnnexesFile();
            BeanUtils.copyProperties(dto, item);
            this.setDefaultValue(item);
            item.setOwnerId(ownerId);
            item.setOwnerType(ownerType);
            insertList.add(item);
        }

        // 删除历史
        this.remove(queryWrapper);

        // 入库
        if (insertList.size() > 0)
            this.saveBatch(insertList);
    }

    /**
     * @param ownerId
     * @param ownerType
     * @return void
     */
    public List<SysAnnexesFileDTO> getList(String ownerId, String ownerType) {
        QueryWrapper<SysAnnexesFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysAnnexesFile::getOwnerId, ownerId);
        queryWrapper.lambda().eq(SysAnnexesFile::getOwnerType, ownerType);

        List<SysAnnexesFile> list = this.list(queryWrapper);
        return convertList(list, SysAnnexesFileDTO.class);
    }
}

